<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>结构化绑定声明 (C++17 起)</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/structured_binding","wgTitle":"cpp/language/structured binding","wgCurRevisionId":72353,"wgArticleId":9180,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/structured_binding","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_structured_binding skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">结构化绑定声明 <span class="t-mark-rev t-since-cxx17">(C++17 起)</span></h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="cpp/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 概览 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations">声明语法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E8.AF.B4.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明说明符序列</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E5.A3.B0.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明符</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 说明符 </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="cpp/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="inline.html" title="cpp/language/inline"><span class="t-lines"><span>inline</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> virtual 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"> explicit 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="friend.html" title="cpp/language/friend"><span class="t-lines"><span>friend</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constexpr.html" title="cpp/language/constexpr"><span class="t-lines"><span>constexpr</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="consteval.html" title="cpp/language/consteval"><span class="t-lines"><span>consteval</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constinit.html" title="cpp/language/constinit"><span class="t-lines"><span>constinit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储类说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="tu_local.html" title="cpp/language/tu local">翻译单元局部</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="class.html" title="cpp/language/class"><span class="t-lines"><span>class</span><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="cpp/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="cpp/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="decltype.html" title="cpp/language/decltype"><span class="t-lines"><span>decltype</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="auto.html" title="cpp/language/auto"><span class="t-lines"><span>auto</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="alignas.html" title="cpp/language/alignas"><span class="t-lines"><span>alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="cv.html" title="cpp/language/cv"><span class="t-lines"><span>const</span><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="elaborated_type_specifier.html" title="cpp/language/elaborated type specifier">详述类型说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> 属性</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 声明符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reference.html" title="cpp/language/reference"> 引用</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="cpp/language/pointer"> 指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="cpp/language/array"> 数组</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 块声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 简单声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink">结构化绑定声明</strong> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> 别名声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> 命名空间别名定义</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="using_declaration.html" title="cpp/language/using declaration"> using 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace"> using 指令</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_assert.html" title="cpp/language/static assert"> static_assert 声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> asm 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum.html" title="cpp/language/enum"> 不可见 enum 声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 其他声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html" title="cpp/language/namespace"> 命名空间定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> 类模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> 函数模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template"> 显式模板实例化</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> 显式模板特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="language_linkage.html" title="cpp/language/language linkage"> 链接说明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 属性声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 空声明</a> </td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>绑定指定名称到初始化器的子对象或元素。
</p><p>类似引用，结构化绑定是既存对象的别名。不同于引用的是，结构化绑定的类型不必为引用类型。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <span class="t-spar">cv-auto</span> <span class="t-spar">引用运算符</span><span class="t-mark">(可选)</span> <code><b>[</b></code> <span class="t-spar">标识符列表</span> <code><b>]</b></code> <code><b>=</b></code> <span class="t-spar">表达式</span> <code><b>;</b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <span class="t-spar">cv-auto</span> <span class="t-spar">引用运算符</span><span class="t-mark">(可选)</span> <code><b>[</b></code> <span class="t-spar">标识符列表</span> <code><b>]</b></code> <code><b>{</b></code> <span class="t-spar">表达式</span> <code><b>}</b></code> <code><b>;</b></code>
</td>
<td> (2)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">属性</span><span class="t-mark">(可选)</span> <span class="t-spar">cv-auto</span> <span class="t-spar">引用运算符</span><span class="t-mark">(可选)</span> <code><b>[</b></code> <span class="t-spar">标识符列表</span> <code><b>]</b></code> <code><b>(</b></code> <span class="t-spar">表达式</span> <code><b>) </b></code> <code><b>;</b></code>
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">属性</span>
</td>
<td> -
</td>
<td>  任意数量的<a href="attributes.html" title="cpp/language/attributes">属性</a>的序列
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">cv-auto</span>
</td>
<td> -
</td>
<td>  可有 cv 限定的 <code><b>auto</b></code> 类型说明符<span class="t-rev-inl t-since-cxx20"><span>，也可以包含<a href="storage_duration.html" title="cpp/language/storage duration">存储类说明符</a> <code><b>static</b></code> 或 <code><b>thread_local</b></code> ；在 cv 限定符中包含 <code><b>volatile</b></code> 是被弃用的</span> <span><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></span></span>
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">引用运算符</span>
</td>
<td> -
</td>
<td>  <code><b>&amp;</b></code> 或 <code><b>&amp;&amp;</b></code> 之一
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">标识符列表</span>
</td>
<td> -
</td>
<td>  此声明所引入的各标识符的逗号分隔的列表
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">表达式</span>
</td>
<td> -
</td>
<td>  顶层没有逗号运算符的表达式（文法上为<i>赋值表达式</i>），且具有数组或非联合类之一的类型。如果 <span class="t-spar">表达式</span> 涉及任何来自 <span class="t-spar">标识符列表</span> 的名字，则声明非良构。
</td></tr></table>
<p>结构化绑定声明将 <span class="t-spar">标识符列表</span> 中的所有标识符引入作为其外围作用域中的名字，并将它们绑定到 <span class="t-spar">表达式</span> 所指代的对象的各个子对象或元素。以此方式引入的绑定被称作<i>结构化绑定</i>。
</p><p>结构化绑定声明首先引入一个唯一命名的变量（此处以 <code><b><i>e</i></b></code> 指代）来保有其初始化器的值，方式如下：
</p>
<ul><li> 如果 <span class="t-spar">表达式</span> 具有数组类型 <code>A</code> 且不存在 <span class="t-spar">引用运算符</span>，则 <code><b><i>e</i></b></code> 具有类型 <i>cv</i> <code>A</code>，其中 <i>cv</i> 是 <span class="t-spar">cv-auto</span> 序列中的 cv 限定符，且 <code><b><i>e</i></b></code> 中的各个元素从 <span class="t-spar">表达式</span> 的对应元素进行复制（对于 <span class="t-v">(1)</span>）或直接（对于 <span class="t-v">(2,3)</span>）初始化。
</li><li> 否则 <code><b><i>e</i></b></code> 如同于声明中以其名取代 <code><b>[</b></code> <span class="t-spar">标识符列表</span> <code><b>]</b></code> 一般定义。
</li></ul>
<p>我们用 <code><b><i>E</i></b></code> 代表表达式 <span class="t-c"><span class="mw-geshi cpp source-cpp">e</span></span> 的类型。（换言之，<code><b><i>E</i></b></code> 等价于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../types/remove_reference.html"><span class="kw607">std::<span class="me2">remove_reference_t</span></span></a><span class="sy1">&lt;</span>decltype<span class="br0">(</span><span class="br0">(</span>e<span class="br0">)</span><span class="br0">)</span><span class="sy1">&gt;</span></span></span>）。
</p><p>然后，结构化绑定可以以下三种方式之一进行绑定，取决于 <code><b><i>E</i></b></code>：
</p>
<ul><li> 情况 1：如果 <code><b><i>E</i></b></code> 是数组类型，则绑定各个名字到各个数组元素。
</li><li> 情况 2：如果 <code><b><i>E</i></b></code> 是非联合类类型且 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/tuple/tuple_size.html"><span class="kw1101">std::<span class="me2">tuple_size</span></span></a><span class="sy1">&lt;</span>E<span class="sy1">&gt;</span></span></span> 是完整类型并拥有名为 <code>value</code> 的成员（无关乎这种成员的类型或可访问性），则使用“元组式”绑定协议。
</li><li> 情况 3：如果 <code><b><i>E</i></b></code> 是非联合类类型但 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/tuple/tuple_size.html"><span class="kw1101">std::<span class="me2">tuple_size</span></span></a><span class="sy1">&lt;</span>E<span class="sy1">&gt;</span></span></span> 不是完整类型，则绑定各个名字到 <code><b><i>E</i></b></code> 的各个可访问数据成员。
</li></ul>
<p>下文对每种情况都有更详细的描述。
</p><p>每个结构化绑定都有一个<i>被引用类型</i>，它会在后文的描述中被定义。此类型是对无括号的结构化绑定应用 <a href="decltype.html" title="cpp/language/decltype"><code>decltype</code></a> 所返回的类型。
</p>
<h4><span class="mw-headline" id=".E6.83.85.E5.86.B5_1.EF.BC.9A.E7.BB.91.E5.AE.9A.E6.95.B0.E7.BB.84"> 情况 1：绑定数组 </span></h4>
<p><span class="t-spar">标识符列表</span> 中的每个标识符均成为指代数组的对应元素的左值。标识符的数量必须等于数组的元素数量。
</p><p>每个标识符的<i>被引用类型</i>都是数组的元素类型。数组类型 <code><b><i>E</i></b></code> 的 cv 限定性与其元素的相同。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> a<span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span><span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw4">auto</span> <span class="br0">[</span>x,y<span class="br0">]</span> <span class="sy1">=</span> a<span class="sy4">;</span> <span class="co1">// 创建 e[2]，复制 a 到 e，然后 x 指代 e[0]，y 指代 e[1]</span>
<span class="kw4">auto</span><span class="sy3">&amp;</span> <span class="br0">[</span>xr, yr<span class="br0">]</span> <span class="sy1">=</span> a<span class="sy4">;</span> <span class="co1">// xr 指代 a[0]，yr 指代 a[1]</span></pre></div></div>
<h4><span class="mw-headline" id=".E6.83.85.E5.86.B5_2.EF.BC.9A.E7.BB.91.E5.AE.9A.E5.85.83.E7.BB.84.E5.BC.8F.E7.B1.BB.E5.9E.8B"> 情况 2：绑定元组式类型 </span></h4>
<p>表达式 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/tuple/tuple_size.html"><span class="kw1101">std::<span class="me2">tuple_size</span></span></a><span class="sy1">&lt;</span>E<span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">value</span></span></span> 必须是良构的整数常量表达式，且标识符的数量必须等于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/tuple/tuple_size.html"><span class="kw1101">std::<span class="me2">tuple_size</span></span></a><span class="sy1">&lt;</span>E<span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">value</span></span></span>。
</p><p>对于每个标识符，引入一个类型为“<span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/tuple/tuple_element.html"><span class="kw1099">std::<span class="me2">tuple_element</span></span></a><span class="sy1">&lt;</span>i, E<span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">type</span></span></span> 的引用”的变量：如果它对应的初始化器是左值，那么它是左值引用，否则它是右值引用。第 i 个变量的初始化器
</p>
<ul><li> 如果在 <code><b><i>E</i></b></code> 的作用域中对标识符 <code>get</code> 按类成员访问进行的查找中，至少找到一个声明是首个模板形参为非类型形参的函数模板，则为 <span class="t-c"><span class="mw-geshi cpp source-cpp">e.<span class="me1">get</span><span class="sy1">&lt;</span>i<span class="sy1">&gt;</span><span class="br0">(</span><span class="br0">)</span></span></span>
</li><li> 否则为 <span class="t-c"><span class="mw-geshi cpp source-cpp">get<span class="sy1">&lt;</span>i<span class="sy1">&gt;</span><span class="br0">(</span>e<span class="br0">)</span></span></span>，其中 <span class="t-c"><span class="mw-geshi cpp source-cpp">get</span></span> 只进行<a href="adl.html" title="cpp/language/adl">实参依赖查找</a>，忽略其他查找。
</li></ul>
<p>这些初始化器表达式中，如果实体 <code><b><i>e</i></b></code> 的类型是左值引用（只会在 <span class="t-spar">引用运算符</span> 是 <code><b>&amp;</b></code>，或它是 <code><b>&amp;&amp;</b></code> 且初始化器是左值时发生），那么 <span class="t-c"><span class="mw-geshi cpp source-cpp">e</span></span> 为左值，否则它是亡值（这实际上进行了一种完美转发），<span class="t-c"><span class="mw-geshi cpp source-cpp">i</span></span> 是 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a></span></span> 的纯右值，而且始终将 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy1">&lt;</span>i<span class="sy1">&gt;</span></span></span> 解释为模板形参列表。
</p><p>该变量拥有与 <code><b><i>e</i></b></code> 相同的<a href="storage_duration.html" title="cpp/language/storage duration">存储期</a>。
</p><p>然后该标识符变成指代与上述变量绑定的对象的左值。
</p><p>第 i 个标识符的<i>被引用类型</i>是 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/tuple/tuple_element.html"><span class="kw1099">std::<span class="me2">tuple_element</span></span></a><span class="sy1">&lt;</span>i, E<span class="sy1">&gt;</span><span class="sy4">::</span><span class="me2">type</span></span></span>。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">float</span> x<span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">char</span>  y<span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span>   z<span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
 
<a href="../utility/tuple.html"><span class="kw1098">std::<span class="me2">tuple</span></span></a><span class="sy1">&lt;</span><span class="kw4">float</span><span class="sy3">&amp;</span>,<span class="kw4">char</span><span class="sy3">&amp;&amp;</span>,<span class="kw4">int</span><span class="sy1">&gt;</span> tpl<span class="br0">(</span>x,std<span class="sy4">::</span><span class="me2">move</span><span class="br0">(</span>y<span class="br0">)</span>,z<span class="br0">)</span><span class="sy4">;</span>
<span class="kw4">const</span> <span class="kw4">auto</span><span class="sy3">&amp;</span> <span class="br0">[</span>a,b,c<span class="br0">]</span> <span class="sy1">=</span> tpl<span class="sy4">;</span>
<span class="co1">// a 指名指代 x 的结构化绑定；decltype(a) 为 float&amp;</span>
<span class="co1">// b 指名指代 y 的结构化绑定；decltype(b) 为 char&amp;&amp;</span>
<span class="co1">// c 指名指代 tpl 的第 3 元素的结构化绑定；decltype(c) 为 const int</span></pre></div></div>
<h4><span class="mw-headline" id=".E6.83.85.E5.86.B5_3.EF.BC.9A.E7.BB.91.E5.AE.9A.E5.88.B0.E6.95.B0.E6.8D.AE.E6.88.90.E5.91.98"> 情况 3：绑定到数据成员 </span></h4>
<p><code><b><i>E</i></b></code> 的所有非静态数据成员必须都是 <code><b><i>E</i></b></code> 或 <code><b><i>E</i></b></code> 的同一基类的直接成员，必须在指名为 <code><i><b>e</b></i>.<i>name</i></code> 时于结构化绑定的语境中是良构的。<code><b><i>E</i></b></code> 不能有匿名联合体成员。标识符的数量必须等于非静态数据成员的数量。
</p><p><span class="t-spar">标识符列表</span> 中的各个标识符，按声明顺序依次成为指代 <code><b><i>e</i></b></code> 的各个成员的左值的名字（支持位域）；该左值的类型是 <span class="t-c"><span class="mw-geshi cpp source-cpp">e.<span class="me1">m_i</span></span></span> 的类型，其中 <code>m_i</code> 指代第 i 个成员。
</p><p>第 i 个标识符的<i>被引用类型</i>是 <span class="t-c"><span class="mw-geshi cpp source-cpp">e.<span class="me1">m_i</span></span></span> ，如果它不是引用类型；否则是 <code>m_i</code> 的声明类型。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> S <span class="br0">{</span>
    mutable <span class="kw4">int</span> x1 <span class="sy4">:</span> <span class="nu0">2</span><span class="sy4">;</span>
    <span class="kw4">volatile</span> <span class="kw4">double</span> y1<span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
S f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
 
<span class="kw4">const</span> <span class="kw4">auto</span> <span class="br0">[</span>x, y<span class="br0">]</span> <span class="sy1">=</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// x 是标识 2 位位域的 int 左值</span>
                         <span class="co1">// y 是 const volatile double 左值</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3"> 注解 </span></h3>
<p>对成员 <code>get</code> 的查找照常忽略可访问性，同时也忽略非类型模板形参的确切类型。出现私有的 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw4">char</span><span class="sy2">*</span><span class="sy1">&gt;</span> <span class="kw4">void</span> get<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span></span></span> 成员将导致使用成员解释方案，即使这会导致程序非良构。
</p><p>声明中 <code><b>[</b></code> 之前的部分应用于隐藏变量 <code><b><i>e</i></b></code>，而非引入的各个标识符。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> a <span class="sy1">=</span> <span class="nu0">1</span>, b <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
<span class="kw4">const</span> <span class="kw4">auto</span><span class="sy3">&amp;</span> <span class="br0">[</span>x, y<span class="br0">]</span> <span class="sy1">=</span> <a href="../utility/tuple/tie.html"><span class="kw1113">std::<span class="me2">tie</span></span></a><span class="br0">(</span>a, b<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// x 与 y 类型为 int&amp;</span>
<span class="kw4">auto</span> <span class="br0">[</span>z, w<span class="br0">]</span> <span class="sy1">=</span> <a href="../utility/tuple/tie.html"><span class="kw1113">std::<span class="me2">tie</span></span></a><span class="br0">(</span>a, b<span class="br0">)</span><span class="sy4">;</span>        <span class="co1">// z 与 w 类型仍为 int&amp;</span>
<a href="../error/assert.html"><span class="kw774">assert</span></a><span class="br0">(</span><span class="sy3">&amp;</span>z <span class="sy1">==</span> <span class="sy3">&amp;</span>a<span class="br0">)</span><span class="sy4">;</span>                    <span class="co1">// 通过</span></pre></div></div>
<p>如果 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/tuple/tuple_size.html"><span class="kw1101">std::<span class="me2">tuple_size</span></span></a><span class="sy1">&lt;</span>E<span class="sy1">&gt;</span></span></span> 是完整类型，那么始终使用元组式解释方案，即使这会导致程序非良构：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> A <span class="br0">{</span> <span class="kw4">int</span> x<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">namespace</span> std <span class="br0">{</span>
    <span class="kw1">template</span><span class="sy1">&lt;&gt;</span> <span class="kw1">struct</span> tuple_size<span class="sy1">&lt;</span><span class="sy4">::</span><span class="me2">A</span><span class="sy1">&gt;</span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">auto</span> <span class="br0">[</span>x<span class="br0">]</span> <span class="sy1">=</span> A<span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 错误；不考虑“数据成员”解释方案。</span></pre></div></div>
<p>如果存在 <span class="t-spar">引用运算符</span> 且 <span class="t-spar">表达式</span> 为纯右值，则应用将引用绑定到临时量的通常规则（包括生存期延续）。这些情况下，隐藏变量 <code><b><i>e</i></b></code> 是绑定到从纯右值表达式<a href="implicit_cast.html#.E4.B8.B4.E6.97.B6.E9.87.8F.E5.AE.9E.E8.B4.A8.E5.8C.96" title="cpp/language/implicit conversion">实质化</a>的临时变量，并延长其生存期的一个引用。与之前一样，如果 <code><b><i>e</i></b></code> 是非 const 左值引用，则绑定失败：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> a <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
<span class="kw4">const</span> <span class="kw4">auto</span><span class="sy3">&amp;</span> <span class="br0">[</span>x<span class="br0">]</span> <span class="sy1">=</span> <a href="../utility/tuple/make_tuple.html"><span class="kw1112">std::<span class="me2">make_tuple</span></span></a><span class="br0">(</span>a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK，非悬垂引用</span>
<span class="kw4">auto</span><span class="sy3">&amp;</span>       <span class="br0">[</span>y<span class="br0">]</span> <span class="sy1">=</span> <a href="../utility/tuple/make_tuple.html"><span class="kw1112">std::<span class="me2">make_tuple</span></span></a><span class="br0">(</span>a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误，不能绑定 auto&amp; 到右值 std::tuple</span>
<span class="kw4">auto</span><span class="sy3">&amp;&amp;</span>      <span class="br0">[</span>z<span class="br0">]</span> <span class="sy1">=</span> <a href="../utility/tuple/make_tuple.html"><span class="kw1112">std::<span class="me2">make_tuple</span></span></a><span class="br0">(</span>a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 同样 OK</span></pre></div></div>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp">decltype<span class="br0">(</span>x<span class="br0">)</span></span></span> 指名结构化绑定的<i>被引用类型</i>，其中 <code>x</code> 代表一个结构化绑定。在元组式的情况下，它是 <span class="t-c"><span class="mw-geshi cpp source-cpp"><a href="../utility/tuple/tuple_element.html"><span class="kw1099">std::<span class="me2">tuple_element</span></span></a></span></span> 所返回的类型，它可以不是引用，即使在此情况下始终会引入隐藏的引用。这相当于模拟了绑定到其各个非静态数据成员具有 <code>tuple_element</code> 所返回的类型的结构体的行为，而绑定自身的引用性质则只是实现细节。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><a href="../utility/tuple.html"><span class="kw1098">std::<span class="me2">tuple</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span>, <span class="kw4">int</span><span class="sy3">&amp;</span><span class="sy1">&gt;</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
 
<span class="kw4">auto</span> <span class="br0">[</span>x, y<span class="br0">]</span> <span class="sy1">=</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>       <span class="co1">// decltype(x) 为 int</span>
                         <span class="co1">// decltype(y) 为 int&amp;</span>
 
<span class="kw4">const</span> <span class="kw4">auto</span> <span class="br0">[</span>z, w<span class="br0">]</span> <span class="sy1">=</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// decltype(z) 为 const int</span>
                         <span class="co1">// decltype(w) 为 int&amp;</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx20"><td>
<p><a href="lambda.html" title="cpp/language/lambda">Lambda 表达式</a>不能捕获结构化绑定。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx20">(C++20 前)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B"> 示例 </span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;set&gt;</span>
<span class="co2">#include &lt;string&gt;</span>
<span class="co2">#include &lt;iomanip&gt;</span>
<span class="co2">#include &lt;iostream&gt;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <a href="../container/set.html"><span class="kw1277">std::<span class="me2">set</span></span></a><span class="sy1">&lt;</span><a href="../string/basic_string.html"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy1">&gt;</span> myset<span class="br0">{</span><span class="st0">"hello"</span><span class="br0">}</span><span class="sy4">;</span>
    <span class="kw1">for</span> <span class="br0">(</span><span class="kw4">int</span> i<span class="br0">{</span><span class="nu0">2</span><span class="br0">}</span><span class="sy4">;</span> i<span class="sy4">;</span> <span class="sy2">--</span>i<span class="br0">)</span> <span class="br0">{</span>
        <span class="kw1">if</span> <span class="br0">(</span><span class="kw4">auto</span> <span class="br0">[</span>iter, success<span class="br0">]</span> <span class="sy1">=</span> myset.<span class="me1">insert</span><span class="br0">(</span><span class="st0">"Hello"</span><span class="br0">)</span><span class="sy4">;</span> success<span class="br0">)</span> 
            <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"插入成功。值为 "</span> <span class="sy1">&lt;&lt;</span> <a href="../io/manip/quoted.html"><span class="kw1802">std::<span class="me2">quoted</span></span></a><span class="br0">(</span><span class="sy2">*</span>iter<span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">"。<span class="es1">\n</span>"</span><span class="sy4">;</span>
        <span class="kw1">else</span>
            <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"集合中已存在值 "</span> <span class="sy1">&lt;&lt;</span> <a href="../io/manip/quoted.html"><span class="kw1802">std::<span class="me2">quoted</span></span></a><span class="br0">(</span><span class="sy2">*</span>iter<span class="br0">)</span> <span class="sy1">&lt;&lt;</span> <span class="st0">"。<span class="es1">\n</span>"</span><span class="sy4">;</span>
    <span class="br0">}</span>
    <span class="kw1">struct</span> BitFields <span class="br0">{</span>
        <span class="kw4">int</span> b <span class="sy4">:</span> <span class="nu0">4</span> <span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span>, d <span class="sy4">:</span> <span class="nu0">4</span> <span class="br0">{</span><span class="nu0">2</span><span class="br0">}</span>, p <span class="sy4">:</span> <span class="nu0">4</span> <span class="br0">{</span><span class="nu0">3</span><span class="br0">}</span>, q <span class="sy4">:</span> <span class="nu0">4</span> <span class="br0">{</span><span class="nu0">4</span><span class="br0">}</span><span class="sy4">;</span>
        <span class="co1">// C++20 ：位域的默认成员初始化器</span>
    <span class="br0">}</span><span class="sy4">;</span>
    <span class="br0">{</span>
        <span class="kw4">const</span> <span class="kw4">auto</span> <span class="br0">[</span>b, d, p, q<span class="br0">]</span> <span class="sy1">=</span> BitFields<span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> b <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> d <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> p <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> q <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="br0">}</span><span class="br0">{</span>
        <span class="kw4">const</span> <span class="kw4">auto</span> <span class="br0">[</span>b, d, p, q<span class="br0">]</span> <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">{</span> <span class="kw1">return</span> BitFields<span class="br0">{</span><span class="nu0">4</span>, <span class="nu0">3</span>, <span class="nu0">2</span>, <span class="nu0">1</span><span class="br0">}</span><span class="sy4">;</span> <span class="br0">}</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
        <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> b <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> d <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> p <span class="sy1">&lt;&lt;</span> <span class="st0">' '</span> <span class="sy1">&lt;&lt;</span> q <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
<p>输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">插入成功。值为 "Hello"。
集合中已存在值 "Hello"。
1 2 3 4
4 3 2 1</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.link/P0961R1">P0961R1</a>
</td>
<td> C++17
</td>
<td> 元组式情况中，如果查找找到任何类型的 <code>get</code> 则使用成员 <code>get</code>
</td>
<td> 仅当查找找到拥有非类型模板形参的函数模板才使用
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.link/P0969R0">P0969R0</a>
</td>
<td> C++17
</td>
<td> 绑定到成员情况中，要求这些成员公开
</td>
<td> 只需要在声明的语境中可访问
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2312">CWG 2312</a>
</td>
<td> C++17
</td>
<td> <code>mutable</code> 的含义在绑定到成员情况中丢失
</td>
<td> 保持其含义
</td></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue2386">CWG 2386</a>
</td>
<td> C++17
</td>
<td> 总是在 <code>tuple_size&lt;E&gt;</code> 为完整类型时使用“元组式”绑定协议
</td>
<td> 只会在 <code>tuple_size&lt;E&gt;</code> 拥有成员 <code>value</code> 时才使用
</td></tr></table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td>  <div class="t-dsc-member-div"><div><a href="../utility/tuple/tie.html" title="cpp/utility/tuple/tie"> <span class="t-lines"><span>tie</span></span></a></div></div>
</td>
<td>   创建左值引用的 <code>tuple</code>，或将 tuple 解包为独立对象 <br> <span class="t-mark">(函数模板)</span> </td></tr>
</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 3674/1000000
Preprocessor generated node count: 9072/1000000
Post‐expand include size: 84466/2097152 bytes
Template argument size: 19201/2097152 bytes
Highest expansion depth: 20/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:9180-0!*!0!!zh!*!zh!* and timestamp 20211125193220 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/structured_binding&amp;oldid=72353">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/structured_binding&amp;oldid=72353</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/structured_binding">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年9月7日 (星期二) 22:47。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过18,389次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 1.779 secs. -->
	</body>
<!-- Cached 20211125193220 -->
</html>